<html>
<head>
    <title>JSONLib Tests</title>
    <!--CSS-->
    <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.4.0/build/logger/assets/logger.css">
    <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.4.0/build/yuitest/assets/testlogger.css">
    
    <!-- Dependencies --> 
    <script type="text/javascript" src="http://yui.yahooapis.com/2.4.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
    <script type="text/javascript" src="http://yui.yahooapis.com/2.4.0/build/logger/logger-min.js"></script>
    
    <!-- Source File -->
    <script type="text/javascript" src="http://yui.yahooapis.com/2.4.0/build/yuitest/yuitest-beta-min.js"></script> 
    <script type="text/javascript" src="jsonlib.js"></script>
</head>
<body>
    <script type="text/javascript">
    
        var suite = new YAHOO.tool.TestSuite("JSONLib Tests");
        suite.add(new YAHOO.tool.TestCase({
        
            name : "JSON Object Tests",
            
            _should : {
            
                error : {
                
                    testPutFunction :  new TypeError("Invalid value data type."),
                    testPutArray :  new TypeError("Invalid value data type."),
                    testPutObject :  new TypeError("Invalid value data type."),
                    testPutInvalidName: new TypeError("Value name must be a string.")
                
                }
            },
            
            testConstructor : function(){            
                var json = new JSON();            
            },
        
            testConstructorWithArg : function(){            
                var json = new JSON({ name: "Nicholas", age: 29 }); 
                YAHOO.util.Assert.areEqual("Nicholas", json.get("name"), "Name value is incorrect.");
                YAHOO.util.Assert.areEqual(29, json.get("age"), "Age value is incorrect.");                
            },
            
            testConstructorAsFunction : function(){            
                var json = JSON({ name: "Nicholas", age: 29 }); 
                YAHOO.util.Assert.areEqual("Nicholas", json.get("name"), "Name value is incorrect.");
                YAHOO.util.Assert.areEqual(29, json.get("age"), "Age value is incorrect.");                
            },
            
            testPut : function(){            
                var json = new JSON();
                json.put("name", "Nicholas");
                json.put("age", 29);
                json.put("found", true);
                json.put("obj", null);                         
            },
            
            testPutInvalidName : function(){
                var json = new JSON();
                json.put(0, "blue");
            },
            
            testPutFunction : function(){
                var json = new JSON();
                json.put("func", function(){});
            },
            
            testPutArray : function(){
                var json = new JSON();
                json.put("array", []);
            },
            
            testPutObject : function(){
                var json = new JSON();
                json.put("obj", {});
            },
            
            testToJSONString : function(){
                var json = new JSON({ name: "Nicholas", age: 29 }); 
                var output = json.toJSONString();
                YAHOO.util.Assert.areEqual("{\"name\":\"Nicholas\",\"age\":29}", output, "JSON string is incorrect.");
            },
        
            testToJSONStringComplex : function(){
                var json = new JSON({ name: "Nicholas", age: 29, found: true, obj: null, msg: "Says, \"hi there!\"", other: "a\nb" }); 
                var output = json.toJSONString();
                YAHOO.util.Assert.areEqual("{\"name\":\"Nicholas\",\"age\":29,\"found\":true,\"obj\":null,\"msg\":\"Says, \\\"hi there!\\\"\",\"other\":\"a\\nb\"}", output, "JSON string is incorrect.");
            },
        
            testToJSONStringComplex2 : function(){
                var json = new JSON(); 
                json.put("name", "Nicholas");
                json.put("age", 29);
                json.put("found", true);
                json.put("obj", null);
                json.put("msg", "Says, \"hi there!\"");
                json.put("other", "a\nb");
                var output = json.toJSONString();
                YAHOO.util.Assert.areEqual("{\"name\":\"Nicholas\",\"age\":29,\"found\":true,\"obj\":null,\"msg\":\"Says, \\\"hi there!\\\"\",\"other\":\"a\\nb\"}", output, "JSON string is incorrect.");
            },
        
            testToJSONStringEmbedded : function(){
                var json = new JSON(); 
                json.put("name", "Nicholas");
                json.put("info", new JSON({ age: 29, found: true }));
                var output = json.toJSONString();
                YAHOO.util.Assert.areEqual("{\"name\":\"Nicholas\",\"info\":{\"age\":29,\"found\":true}}", output, "JSON string is incorrect.");
            }

        }));
    
        suite.add(new YAHOO.tool.TestCase({
        
            name : "JSONList Object Tests",
            
            _should : {
            
                error : {
                
                    testPutFunction :  new TypeError("Invalid value data type."),
                    testPutArray :  new TypeError("Invalid value data type."),
                    testPutObject :  new TypeError("Invalid value data type."),
                    testPutInvalidIndex: new TypeError("Index must be a number.")
                
                }
            },
            
            testConstructor : function(){            
                var json = new JSONList();            
            },
        
            testConstructorWithArg : function(){            
                var json = new JSONList(["Nicholas","Zakas"]); 
                YAHOO.util.Assert.areEqual("Nicholas", json.get(0), "First value is incorrect.");
                YAHOO.util.Assert.areEqual("Zakas", json.get(1), "Second value is incorrect.");                
            },
            
            testConstructorAsFunction : function(){            
                var json = JSONList(["Nicholas","Zakas"]); 
                YAHOO.util.Assert.areEqual("Nicholas", json.get(0), "First value is incorrect.");
                YAHOO.util.Assert.areEqual("Zakas", json.get(1), "Second value is incorrect.");                
            },
            
            testPut : function(){            
                var json = new JSONList();
                json.put(0, "Nicholas");
                json.put(1, 29);
                json.put(2, true);
                json.put(3, null);    
                YAHOO.util.Assert.areEqual(4, json.getLength(), "Length is incorrect.");                
            },
            
            testPutInvalidIndex : function(){            
                var json = new JSONList();
                json.put("blue", "Nicholas");  
            },
            
            testPutFunction : function(){
                var json = new JSONList();
                json.put(0, function(){});
            },
            
            testPutArray : function(){
                var json = new JSONList();
                json.put(0, []);
            },
            
            testPutObject : function(){
                var json = new JSONList();
                json.put(0, {});
            },
            
            testPush : function(){
                var json = new JSONList();
                json.push("Nicholas");
                json.push(0);
                
                YAHOO.util.Assert.areEqual("Nicholas", json.get(0), "First value is not correct.");
                YAHOO.util.Assert.areEqual(0, json.get(1), "Second value is not correct.");
                YAHOO.util.Assert.areEqual(2, json.getLength(), "First value is not correct.");                
            },
            
            testPop : function(){
                var json = new JSONList(["Nicholas", 0]);
                var value = json.pop();
                
                YAHOO.util.Assert.areEqual("Nicholas", json.get(0), "First value is not correct.");
                YAHOO.util.Assert.areEqual(0, value, "Popped value is not correct.");
                YAHOO.util.Assert.areEqual(1, json.getLength(), "First value is not correct.");                
            },
            
            testShift : function(){
                var json = new JSONList(["Nicholas", 0]);
                var value = json.shift();
                
                YAHOO.util.Assert.areEqual("Nicholas", value, "Shifted value is not correct.");
                YAHOO.util.Assert.areEqual(0, json.get(0), "Popped value is not correct.");
                YAHOO.util.Assert.areEqual(1, json.getLength(), "First value is not correct.");                
            },
            
            testUnshift : function(){
                var json = new JSONList();
                json.unshift("Nicholas");
                json.unshift(0);
                
                YAHOO.util.Assert.areEqual("Nicholas", json.get(1), "Second value is not correct.");
                YAHOO.util.Assert.areEqual(0, json.get(0), "First value is not correct.");
                YAHOO.util.Assert.areEqual(2, json.getLength(), "First value is not correct.");                
            },
            
            testToJSONString : function(){
                var json = new JSONList(["Nicholas","Zakas"]); 
                var output = json.toJSONString();
                YAHOO.util.Assert.areEqual("[\"Nicholas\",\"Zakas\"]", output, "JSON string is incorrect.");
            },
        
            testToJSONStringComplex : function(){
                var json = new JSONList(["Nicholas", 29, true, null, "Says, \"hi there!\"", "a\nb" ]); 
                var output = json.toJSONString();
                YAHOO.util.Assert.areEqual("[\"Nicholas\",29,true,null,\"Says, \\\"hi there!\\\"\",\"a\\nb\"]", output, "JSON string is incorrect.");
            },
        
            testToJSONStringEmbedded : function(){
                var json = new JSONList(); 
                json.put(0, "Nicholas");
                json.put(1, new JSONList([29, true, null]));
                var output = json.toJSONString();
                YAHOO.util.Assert.areEqual("[\"Nicholas\",[29,true,null]]", output, "JSON string is incorrect.");
            }

        }));        
        
        suite.add(new YAHOO.tool.TestCase({
        
            name : "JSON Parsing Tests",
            
            _should : {
            
                error : {
                
                    testParseInvalidArray :  true,
                    testParseInvalidObject: true
                
                }
            },
            
            testParseSimpleObject : function(){
                var obj = JSON.parse("{\"name\":\"Nicholas\", \"age\" : 29 , \"found\" :true, \"obj\": null}");
                
                YAHOO.util.Assert.isInstanceOf(JSON, obj, "Returned object should be JSON.");
                YAHOO.util.Assert.areEqual("Nicholas", obj.get("name"), "Property 'name' should be 'Nicholas'.");
                YAHOO.util.Assert.areEqual(29, obj.get("age"), "Property 'age' should be 29.");      
                YAHOO.util.Assert.areEqual(true, obj.get("found"), "Property 'found' should be true.");
                YAHOO.util.Assert.areEqual(null, obj.get("obj"), "Property 'obj' should be null.");
            },
            
            testParseSimpleArray : function(){
                var obj = JSON.parse("[\"Nicholas\",  29 , true, null]");
                
                YAHOO.util.Assert.isInstanceOf(JSONList, obj, "Returned object should be JSONList.");
                YAHOO.util.Assert.areEqual("Nicholas", obj.get(0), "Property '0' should be 'Nicholas'.");
                YAHOO.util.Assert.areEqual(29, obj.get(1), "Property '1' should be 29.");      
                YAHOO.util.Assert.areEqual(true, obj.get(2), "Property '2' should be true.");
                YAHOO.util.Assert.areEqual(null, obj.get(3), "Property '3' should be null.");
            },
            
            testParseInvalidArray : function(){
                var obj = JSON.parse("[\"Nicholas\",  29 , true, null}");
            },
            
            testParseInvalidObject : function(){
                 var obj = JSON.parse("{\"name\":\"Nicholas\", \"age\" : 29 , \"found\" :true, \"obj\": null]");           
            }
        }));
            
        
    
        window.onload = function(){
        
            new YAHOO.tool.TestLogger();
            YAHOO.tool.TestRunner.add(suite);
            YAHOO.tool.TestRunner.run();
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        }
    
    </script>
</body>
</html>